<?
  class ManageController extends CFD_Controller_Action
  {
    public function init()
	{
	  parent::init();
	  $this->isAdmin(true);
	}

	public function statsAction()	{
		$this->view->title = "System stats";
		
		$voucherTable = new Application_Model_Vouchers ();
		$res = $voucherTable->getAdapter ()->query ( "SELECT COUNT(*) as count FROM vouchers" );
		$count = $res->fetchAll ();
		$this->view->allVouchers = $count [0] ['count'];
		
		$voucherTable = new Application_Model_Vouchers ();
		$res = $voucherTable->getAdapter ()->query ( "SELECT COUNT(*) as count FROM vouchers where purchase_users_id != 0" );
		$count = $res->fetchAll ();
		$this->view->purchased = $count [0] ['count'];
		
		$voucherTable = new Application_Model_Vouchers ();
		$res = $voucherTable->getAdapter ()->query ( "SELECT COUNT(*) as count FROM vouchers where exp_date < '" . date ( "Y-m-d" ) . "'" );
		$count = $res->fetchAll ();
		$this->view->purchased = $count [0] ['count'];
		
		$voucherTable = new Application_Model_Vouchers ();
		$res = $voucherTable->getAdapter ()->query ( "SELECT COUNT(*) as count FROM vouchers where exp_date >= '" . date ( "Y-m-d" ) . "'" );
		$count = $res->fetchAll ();
		$this->view->active = $count [0] ['count']; 		$voucherTable = new Application_Model_Vouchers();		$res = $voucherTable->getAdapter()->query("SELECT COUNT(*) as count FROM vouchers where exp_date >= '".date("Y-m-d")."'");		$count = $res->fetchAll();		$this->view->active = $count[0]['count'];		$valTable = new Application_Model_Vouchers();		$select = $valTable ->select();		$this->view->valuationsListFiltered = $this->view->valuationsList = $valTable->fetchAll($select);	}
	public function addVoucherCodeAction()	{		$this->view->title = "Add voucher to the datbase";		$this->view->hideResult = true;				$voucherTable = new Application_Model_Vouchers();		if ($_GET['id']) {			$row = $voucherTable->fetchRow( 'id=' . $_GET['id'] );			if ($row)			{				$this->view->editData = $row->toArray();			}			$this->view->vid = $_GET['id'];		}
		if ($_POST['voucher'])		{			if ($_POST['vid']) {				$row = $voucherTable->fetchRow( $voucherTable->select()->where('id = ?', $_POST['vid']) );			}						$rowtest = $voucherTable->fetchRow( $voucherTable->select()->where('voucher = ?', $_POST['voucher']) );			if ($rowtest) {				Alert_View::addMessage(Alert_View::NOTICE, "", "Voucher number exist in the database");				return;			}			$row = $voucherTable->createRow();								$row->voucher = $_POST['voucher'];			$row->exp_date = $_POST['exp_date'];			$row->name = $_POST['name'];			$row->state = $_POST['state'];			$row->city = $_POST['city'];			$row->zip = $_POST['zip'];			$row->price = $_POST['price'];			$row->save();			Alert_View::addMessage(Alert_View::NOTICE, "", "New voucher were successfully loaded to the database");					}	}		public function usersAction()
	{
	  $this->view->title = "Manage users";

	  $usersTable = new Application_Model_Users();

	  if ($_GET['ch'])
	  {
	    $usersTable = new Application_Model_Users();
		if ($usersTableRow = $usersTable->find((int)$_GET['ch']))
		{
  		  $usersTableRow[0]->user_enabled = !($usersTableRow[0]->user_enabled);
  		  $usersTableRow[0]->save();
		}
	  }

	  $adapter = new Zend_Paginator_Adapter_DbSelect($usersTable->select()->order('id DESC'));

	  $this->view->page = $this->_request->getParam('page');

	  if (!$this->view->page)
	    $this->view->page = 1;

	  $res = $usersTable->getAdapter()->query("SELECT COUNT(*) as count FROM users");
	  $count = $res->fetchAll();
	  $count = $count[0]['count'];

	  $adapter->setRowCount( (int)$count );

	  $paginator = $this->view->paginator = new Zend_Paginator($adapter);
	  $paginator->setItemCountPerPage(7);
	  $paginator->setCurrentPageNumber($this->_request->getParam('page'));
	}	public function deleteAction() {		$id = $_GET['id'];		if ($id == '')			return;		$this->isAdmin(true);		$voucherTable = new Application_Model_Vouchers();		$res = $voucherTable->getAdapter()->query("delete FROM vouchers where id = " . addslashes($id));		$this->getResponse()->setRedirect('/manage/stats');	}	
  }
